<?php

namespace app\admin\controller\box\address;

use app\common\controller\Backend;

/**
 * 小区商品
 *
 * @icon fa fa-circle-o
 */
class Housegoods extends Backend {

    /**
     * Goods模型对象
     * @var \app\admin\model\house\Goods
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\house\Goods;
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $otherwhere[] = ['is_del', '=', 0];
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, null, $otherwhere, ['goods_id' => 'fa_house_goods.goods_id']);
            $total = $this->model
                    ->where($where)
                    ->join('fa_goods', 'fa_house_goods.goods_id = fa_goods.goods_id', 'left')
                    ->join('fa_goods_brand', 'fa_goods_brand.brand_id = fa_goods.brand_id', 'left')
                    ->join('fa_goods_cate', 'fa_goods_cate.cate_id = fa_goods.cate_id', 'left')
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->field('fa_house_goods.*,fa_goods.goods_logo,fa_goods.goods_title,fa_goods.goods_model,fa_goods.goods_spec,fa_goods.goods_weight,fa_goods.purchase_price,fa_goods.selling_price as goods_price,fa_goods_brand.brand_title,fa_goods_cate.cate_title')
                    ->join('fa_goods', 'fa_house_goods.goods_id = fa_goods.goods_id', 'left')
                    ->join('fa_goods_brand', 'fa_goods_brand.brand_id = fa_goods.brand_id', 'left')
                    ->join('fa_goods_cate', 'fa_goods_cate.cate_id = fa_goods.cate_id', 'left')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "") {
        if ($ids) {

            $hdata['is_del'] = 1;
            $hdata['updatetime'] = date('Y-m-d H:i:s', time());
            db('house_goods')->where('house_goods_id', 'in', $ids)->update($hdata);
            $goods_ids = db('house_goods')->where('house_goods_id', 'in', $ids)->column('goods_id');
            db('user_box_goods')->where('goods_id', 'in', $goods_ids)->update($hdata);

            $user_box_goods_ids = db('user_box_goods')->where('goods_id', 'in', $goods_ids)->column('user_box_id');

            db('cart')->where('user_box_id', 'in', $user_box_goods_ids)->delete();
            $this->success();
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    public function editnum() {

        $house_goods_id = input('house_goods_id', 0, 'intval');
        $arrData['selling_price'] = input('selling_price', 0);

        db('house_goods')->where(['house_goods_id' => $house_goods_id])->update($arrData);

        $goods_id = input('goods_id', 0, 'intval');
        $house_id = input('house_id', 0, 'intval');

        $user_box_ids = db('user_box')->where('quarters_id', $house_id)->column('user_box_id');
        db('user_box_goods')->where('user_box_id','in',$user_box_ids)->where('goods_id',$goods_id)->update($arrData);
        $box_goods_ids = db('user_box_goods')->where('user_box_id','in',$user_box_ids)->where('goods_id',$goods_id)->column('box_goods_id');
        db('cart')->where('box_goods_id','in',$box_goods_ids)->update($arrData);
        $this->success();
    }

}
